package com.lminkey.servlet;

import com.lmonkey.dao.Basedao;
import com.lmonkey.dao.MD5;
import com.lmonkey.servic.USERDao;
import com.lmonkey.shop.USER;
import org.apache.taglibs.standard.extra.spath.Step;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet( "/admin/admin_douserupdate")
public class DoUserUdate extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html/charset=utf-8");
        //获取usermodify中的参数
        String userid = request.getParameter("userid");
        String username = request.getParameter("username");
        String name = request.getParameter("name");
        String pass = request.getParameter("password");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String email = request.getParameter("email");
        String mobile = request.getParameter("mobile");
        String status = request.getParameter("userstatus");
        String address = request.getParameter("address");

        int userstatus = 1;
        if (status != null)
        {
             userstatus = Integer.parseInt(status);
        }
        //创建用户实体
        USER user = new USER(username,name, pass, sex, birthday, null, email, mobile, address, userstatus);
        //修改到数据库
        Connection conn = Basedao.getconn();
        PreparedStatement ps = null;
        int count = 0;
        try{
            String sql = "update lmonkeyshop set user_id=?,user_name=?,user_sex=?,user_birthday=DATE_FORMAT(?,'%Y-%m-%d'),user_email=?,user_mobile=?,user_address=?,user_status=? where user_id = ?";
            ps = conn.prepareStatement(sql);
            ps.setObject(1,user.getuser_id());
            ps.setObject(2,user.getuser_name());
            ps.setObject(3,user.getuser_sex());
            ps.setObject(4,user.getuser_birthday());
            ps.setObject(5,user.getuser_email());
            ps.setObject(6,user.getuser_mobile());
            ps.setObject(7,user.getuser_address());
            ps.setObject(8,user.getuser_status());
            ps.setString(9,userid);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //进行判断
        if (count>0){
            response.sendRedirect("admin_douserselect?cp="+request.getParameter("cpage")+"&keywords="+request.getParameter("keywords"));
        }else{
            PrintWriter out = response.getWriter();
            out.println("<script>alert('用户修改失败');location.href='/lyq/admin/admin_touserupdate?id="+userid+"';</script>");
            out.close();
        }
    }
}
